//
//  MainViewController.m
//  Cat
//
//  Created by lushitong on 2018/11/29.
//  Copyright © 2018年 lushitong. All rights reserved.
//

#import "MainViewController.h"
#import "QKPhoneInfo.h"
#import "HomeViewController.h"
#import "MeViewController.h"
#import "AssortViewController.h"
#import "MainManager.h"

#import <AudioToolbox/AudioToolbox.h>

@interface MainViewController ()<QKTabBarDelegate>

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
	self.customTabBar.delegate = self;
	self.needRedPoint = YES;
	[self addAllChildVC];
}

- (void)addAllChildVC {
	HomeViewController *home = [[HomeViewController alloc] init];	
	[self addChildViewController:home andTitle:@"首页" andImageName:@"tab_home_nm" andSelectImageName:@"tab_home_sel"];
	[self addChildViewController:[AssortViewController new] andTitle:@"分类" andImageName:@"tab_assort_nm" andSelectImageName:@"tab_assort_sel"];
	[self addChildViewController:[MeViewController new] andTitle:@"我的" andImageName:@"tab_me_nm" andSelectImageName:@"tab_me_sel"];
	self.selectedIndex = UDToken.length == 0 ? 2 : 0;
	[self.customTabBar selectIndex:UDToken.length == 0 ? 2 : 0];
}

- (void)viewDidAppear:(BOOL)animated {
	[super viewDidAppear:animated];
	[self initTabBarItemBadge];
}

/// 震动
- (void)shakingPhoneAction {
	if (@available(iOS 10.0, *)) {
		if ([[QKPhoneInfo iphoneType] isEqualToString:@"iPhone 7"]
			|| [[QKPhoneInfo iphoneType] isEqualToString:@"iPhone 7 Plus"]
			|| [[QKPhoneInfo iphoneType] isEqualToString:@"iPhone 8"]
			|| [[QKPhoneInfo iphoneType] isEqualToString:@"iPhone 8 Plus"]
			|| [[QKPhoneInfo iphoneType] isEqualToString:@"iPhone X"]
			|| [[QKPhoneInfo iphoneType] isEqualToString:@"iPhone XS"]
			|| [[QKPhoneInfo iphoneType] isEqualToString:@"iPhone XS Max"]) {
			UIImpactFeedbackGenerator *feedBackGenertor = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleMedium];
			[feedBackGenertor impactOccurred];
		} else {
			AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
		}
	} else {
		AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
	}
}

/// 设置TabBar红点
- (void)initTabBarItemBadge {
	dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
		// 我的
		if (!UDRedPoint && UDToken.length > 0) {
			[MainManager addBadgeWithIndex:TabbarIndexTypeMe tabBarController:self];
		} else {
			[MainManager removeBadgeWithIndex:TabbarIndexTypeMe tabBarController:self];
		}
	});
}

@end
